package com.wzw.proxy;

import com.wzw.aspect.MyAspect;
import com.wzw.dao.IUserDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

@Component
public class JdkProxy implements InvocationHandler {
    private IUserDao userDao;

    @Autowired
    private MyAspect myAspect = null;

    public Object createProxy(IUserDao userDao){
        this.userDao = userDao;
        //得到类加载器
        ClassLoader classLoader = JdkProxy.class.getClassLoader();
        //获取所有接口
        Class[] clazz = userDao.getClass().getInterfaces();
        //this 代理对象
        return Proxy.newProxyInstance(classLoader, clazz, this);
    }
    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        myAspect.checkPermision();
        //调用方法，传对应参数
        Object obj = method.invoke(userDao, args);

        myAspect.log();
        return obj;
    }
}
